package com.yidaozou.dao.jdbc.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import com.yidaozou.dao.exception.DaoException;
import com.yidaozou.dao.jdbc.IJDBCBaseDao;

public class JDBCBaseDaoImpl<T> implements IJDBCBaseDao<T>{
	
	@Autowired
	private JdbcTemplate jdbcTemplate;

	public int save(String sql) throws DaoException {
		try {
			jdbcTemplate.execute(sql);
			return 1;
		} catch (Exception e) {
			throw new DaoException("Execute save by JDBC has been error!", e);
		}
	}

	public int delete(String sql) throws DaoException {
		try {
			jdbcTemplate.execute(sql);
			return 1;
		} catch (Exception e) {
			throw new DaoException("Execute delete by JDBC has been error!", e);
		}
	}

	public int merge(String sql) throws DaoException{
		try {
			jdbcTemplate.execute(sql);
			return 1;
		} catch (Exception e) {
			throw new DaoException("Execute update by JDBC has been error!", e);
		}
	}

	public List<Map<String, Object>> find(String sql) throws DaoException {
		try {
			return jdbcTemplate.queryForList(sql);
		} catch (Exception e) {
			throw new DaoException("Execute find by JDBC has been error!", e);
		}
	}
}
